package com.mo.ld.activity.view

import android.Manifest
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.XXPermissions
import com.mo.ld.bean.ActionBean
import com.mo.ld.modle.getMainAdapter2
import com.mo.lib.ui.activity.BaseRvActivity
import com.mo.lib.utils.*

/**
 * @ author：mo
 * @ data：2022/4/20:14:01
 * @ 功能：
 */

class BroadcastReceiverActivity : BaseRvActivity<ActionBean>() {
    private var gps: BaseReceiver? = null
    private var sys: BaseReceiver? = null
    private var screen: BaseReceiver? = null
    private var net: BaseReceiver? = null

    override fun onDestroy() {
        super.onDestroy()
        gps?.let {
            unregisterBroadcast(it)
        }
        sys?.let {
            unregisterBroadcast(it)
        }
        screen?.let {
            unregisterBroadcast(it)
        }
    }

    override fun getList(page: Int) {
    }

    override fun getMore(page: Int) {
    }

    override fun creatAdaper(): BaseQuickAdapter<ActionBean, BaseViewHolder> {
        return getMainAdapter2(mutableListOf(
            ActionBean("开GPS广播"){
                XXPermissions.with(this).permission(Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION).request(object : OnPermissionCallback {
                    override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                        if (all) {
                            gps = registerBroadcastGps(onRecevive = {
                                it?.let {
                                    it.onGspChangeListener() {
                                        KLog.i("GPS状态发生变化   当前开关状态 ：${it}")
                                    }
                                }
                            })
                        }
                    }
                })
            },
            ActionBean("开Home键广播"){
                sys = registerBroadcastHome( onRecevive = {
                    it?.let {
                        it.onHomeTouchListener() {
                            when (it) {
                                1 -> {
                                    KLog.i("短按Home键   ")
                                }
                                2 -> {
                                    KLog.i("多任务键(单home的时候长按)   ")
                                }
                                3 -> {
                                    KLog.i("samsung 长按Home键   ")
                                }
                            }
                        }
                    }
                })
            },
            ActionBean("开屏广播"){
                screen = registerBroadcastScreen(onRecevive = {
                    it?.let {
                        it.onScreenListener() {
                            when (it) {
                                1 -> {
                                    KLog.i("开屏   ")
                                }
                                2 -> {
                                    KLog.i("锁屏 ")
                                }
                                3 -> {
                                    KLog.i("解锁 ")
                                }
                            }
                        }
                    }
                })
            },
            ActionBean("网络状态广播"){
                net = registerBroadcastNet(onRecevive = {
                    it?.let {
                        it.onNetChangeListener() {
                            KLog.i("是否联网   ${it}   ")
                        }
                    }
                })
            },))
        }
}